package com.czl.module_storehouse.activity.operation;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.czl.base.data.bean.tengyun.LocatListBean;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.activity.BaseActivity;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.databinding.ActivitySelectedProductBinding;
import com.czl.module_storehouse.databinding.HeaderSelectedSortBinding;
import com.czl.module_storehouse.adapter.SelectedSortAdapter;
import com.czl.module_storehouse.event.SelectedProductEvent;
import com.czl.module_storehouse.event.SelectedProductResultEvent;
import com.czl.module_storehouse.mvp.presenter.SelectedSortPresenter;
import com.czl.module_storehouse.mvp.view.SelectedSortView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author by xubin,
 * @date on 2022/2/17.
 * PS: Not easy to write code, please indicate.
 */
public class SelectedSortActivity extends BaseActivity<ActivitySelectedProductBinding> implements SelectedSortView {

    private HeaderSelectedSortBinding mHeaderBinding;
    private SortBean mSortBean;
    private SelectedSortAdapter mAdapter;
    @InjectPresenter
    SelectedSortPresenter mSelectedSortPresenter;

    @Override
    protected ActivitySelectedProductBinding getBinding(LayoutInflater layoutInflater) {
        return ActivitySelectedProductBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mHeaderBinding = HeaderSelectedSortBinding.inflate(getLayoutInflater(), binding.layoutTop, true);
        mAdapter = new SelectedSortAdapter(R.layout.item_receive_sort);
        binding.textAdd.setOnClickListener(this);
        binding.recyclerView.setAdapter(mAdapter);
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(SelectedProductEvent event) {
        if (event == null || event.getSortBean() == null) {
            return;
        }
        mSortBean = event.getSortBean();
        String sortName = mSortBean.getSortName();
        Integer applyNum = mSortBean.getApplyNum();
        if (applyNum == null || applyNum == 0) {
            mHeaderBinding.tvTitle.setText(sortName);
        } else {
            String text = sortName + "（" + applyNum + "）";
            mHeaderBinding.tvTitle.setText(text);
        }

        mAdapter.setMaxCount(event.getMaxNum());

        String title = event.getTitle();
        if (title == null) {
            title = "";
        }
        mAdapter.setTitle(title);
        mToolBinding.toolbarContentTitle.setText(getString(R.string.store_title_goods, title));
    }

    @Override
    protected void loadData() {
        super.loadData();
        if (mSortBean == null) {
            return;
        }
        mSelectedSortPresenter.getStorehouseSortStockList(mSortBean.getSortId());
    }

    @Override
    public void showEmpty() {
        super.showEmpty();
        mAdapter.removeEmptyView();
        View emptyView = View.inflate(getContext(), com.czl.module_base.R.layout.common_empty_layout, null);
        emptyView.findViewById(com.czl.module_base.R.id.ll_empty).setOnClickListener(v -> loadData());
        mAdapter.setEmptyView(emptyView);
    }

    @Override
    public <T> void showData(T t) {
        super.showData(t);
        if (t == null) {
            showEmpty();
            return;
        }
        ListBean<LocatListBean> listBean = (ListBean<LocatListBean>) t;
        List<LocatListBean> list = listBean.getList();
        if (list == null || list.size() == 0) {
            showEmpty();
            return;
        }
        if (mSortBean != null) {
            List<LocatListBean> locatList = mSortBean.getLocatList();
            if (locatList != null) {
                for (LocatListBean locatListBean : list) {
                    for (LocatListBean bean : locatList) {
                        if (locatListBean.getStorehouseLocatId().equals(bean.getStorehouseLocatId())) {
                            locatListBean.setAddNum(bean.getStockNum());
                            locatListBean.setShiftNum(bean.getAddNum());
                        }
                    }
                }
            }
            int totalNum = 0;
            for (LocatListBean bean : list) {
                totalNum += bean.getStockNumInt();
            }
            mHeaderBinding.tvTitle.setText(getString(R.string.sort_title_placeholder, mSortBean.getSortName(), totalNum));
        }
        mAdapter.setList(list);
    }

    @Override
    public void onClick(View view) {
        List<LocatListBean> data = mAdapter.getData();
        List<LocatListBean> list = new ArrayList<>();
        for (LocatListBean bean : data) {
            int addNum = bean.getAddNum();
            if (addNum > 0) {
                bean.setStockNum(addNum);
                list.add(bean);
            }
        }
        mSortBean.setLocatList(list);
        EventBus.getDefault().postSticky(new SelectedProductResultEvent());
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHeaderBinding != null) {
            mHeaderBinding.unbind();
        }
    }
}
